/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haiberg.osgi.bundle.httpdemo.tracker;

import com.haiberg.osgi.bundle.httpdemo.servlet.IndexServlet;
import com.haiberg.osgi.bundle.utils.FileUtils;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.ServletException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.util.tracker.ServiceTracker;

/**
 *
 * @author gyue
 */
public class HttpServiceTracker extends ServiceTracker{
    
    public HttpServiceTracker(BundleContext context){
        super(context, HttpService.class.getName(), null);
    }

    @Override
    public Object addingService(ServiceReference reference) {
        System.out.println("com.haiberg.osgi.bundle.httpdemo.tracker.HttpServiceTracker.addingService()");
        HttpService httpService = (HttpService) this.context.getService(reference);
        try {
            String resourcePath = FileUtils.getBundleResourcePath(this.context.getBundle(),"httpdemo","webapps");
            httpService.registerResources("/static", resourcePath,null);
            httpService.registerServlet("/index", new IndexServlet(),null, null);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (NamespaceException e) {
            e.printStackTrace();
        }
        return httpService;
    }

    @Override
    public void removedService(ServiceReference reference, Object service) {
        System.out.println("com.haiberg.osgi.bundle.httpdemo.tracker.HttpServiceTracker.removedService()");
    }
}
